package cn.tedu.sp04.order.service;

import cn.tedu.sp01.entity.Item;
import cn.tedu.sp01.entity.Order;
import cn.tedu.sp01.entity.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp04.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 作者：小龙猿
 * @Project 项目：springcloud1
 * @Time 时间：2021/9/9 15:41
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private UserClient userClient;
    @Autowired
    private ItemClient itemClient;
    @Override
    public Order getOrder(String orderId) {

        //TODO: 远程调用user-service获取用户信息
        JsonResult<User> user = userClient.getUser(8);//这儿是自己设定的值,真实项目中应该根据登录的用户id查询
//浏览器-->OrderController-->OrderServiceImpl-->ItemClient-->ItemController-->ItemServiceImpl
        //TODO: 远程调用item-service获取商品信息
        JsonResult<List<Item>> items = itemClient.getItem(orderId);
        Order order = new Order();
        order.setId(orderId);
        order.setUser(user.getData());
        order.setItems(items.getData());
        return order;
    }

    @Override
    public void addOrder(Order order) {
        //TODO: 调用item-service减少商品库存
        itemClient.decreaseNumber(order.getItems());
        //TODO: 调用user-service增加用户积分
        userClient.addScore(order.getUser().getId(), 100);
        log.info("保存订单: "+order);
    }
}
